cmake_minimum_required(VERSION 3.14)
if (ENABLE_OPEN_SRC)
    find_package(c_base CONFIG REQUIRED)
endif()

set(INSTALL_LIBRARY_DIR lib)
set(SRC_LIST
    common/acl.c
    runtime/callback.c
    runtime/context.c
    runtime/stream.c
    runtime/device.c
    runtime/memory.c
    runtime/host_func.c
    model/model_config.c
    model/model_exec_config.c
    model/model.c
    model/model_desc.c
    types/tensor_desc_internal.c
)

add_library(ascendcl_c SHARED ${SRC_LIST})

target_include_directories(ascendcl_c PRIVATE
if(NOT ENABLE_OPEN_SRC)
    ${TOP_DIR}/inc
    ${TOP_DIR}/inc/external
else()
    $<$<BOOL:${ENABLE_OPEN_SRC}>:${ASCEND_OPENSDK_DIR}/include/>
    $<$<BOOL:${ENABLE_OPEN_SRC}>:${ASCEND_OPENSDK_DIR}/include/air>
    $<$<BOOL:${ENABLE_OPEN_SRC}>:${ASCEND_OPENSDK_DIR}/include/runtime>
    $<$<BOOL:${ENABLE_OPEN_SRC}>:${ASCEND_OPENSDK_DIR}/include/msprof>
    $<$<BOOL:${ENABLE_OPEN_SRC}>:${ASCEND_OPENSDK_DIR}/include/slog>
endif()
    ${BASE_DIR}/c/
    ${BASE_DIR}/c/common/
    ${BASE_DIR}/inc/external
    ${BASE_DIR}/inc/external/acl
    ${BASE_DIR}/inc
)

target_compile_options(ascendcl_c PRIVATE
    $<$<STREQUAL:${TARGET_SYSTEM_NAME},Linux>:-fvisibility=hidden -O2 -fno-common>
    $<$<STREQUAL:${TARGET_SYSTEM_NAME},Windows>:/utf-8 /Od>
    $<$<AND:$<STREQUAL:${TARGET_SYSTEM_NAME},Windows>,$<STREQUAL:${CMAKE_CONFIGURATION_TYPES},Debug>>:/MTd>
    $<$<AND:$<STREQUAL:${TARGET_SYSTEM_NAME},Windows>,$<STREQUAL:${CMAKE_CONFIGURATION_TYPES},Release>>:/MT>
)

target_compile_definitions(ascendcl_c PRIVATE
    FUNC_VISIBILITY
    RUN_TEST
)

target_link_libraries(ascendcl_c PRIVATE
    $<BUILD_INTERFACE:intf_pub>
    $<$<NOT:$<BOOL:${ENABLE_OPEN_SRC}>>:$<BUILD_INTERFACE:msprof_headers>>
    c_sec_headers
    -Wl,--no-as-needed
    ge_executor_c
    -Wl,--as-needed
    slog_headers
    -s
)

target_link_options(ascendcl_c PRIVATE
    -rdynamic
    -Wl,-Bsymbolic
    -Wl,--exclude-libs,ALL
)

set_target_properties(ascendcl_c PROPERTIES
    OUTPUT_NAME ascendcl
)

install(TARGETS  ascendcl_c OPTIONAL
    LIBRARY DESTINATION ${INSTALL_LIBRARY_DIR}/c
)